package com.atguigu.ssyx.common.result;

import lombok.Data;

/**
 * @author 杨林
 * @create 2023-07-04 21:57 星期二
 * description:
 */
@Data
public class Result<T> {

    //状态码
    private Integer code;

    //信息
    private String message;

    //数据
    private T data;

    //构造器私有化
    private Result(){}

    //设置数据，返回对象的方法
    public static<T> Result<T> build(T data,Integer code,String message){
        //创建Result对象
        Result<T> result = new Result<>();
        if (data != null){
            result.setData(data);
        }
        //设置其他信息
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static<T> Result<T> build(T data,ResultCodeEnum resultCodeEnum){
        //创建Result
        Result<T> result = new Result<>();
        if (data != null){
            result.setData(data);
        }
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    //成功
    public static Result ok(){
        Result result = build(null, ResultCodeEnum.SUCCESS);
        return result;
    }

    public static<T> Result<T> ok(T data){
        Result<T> result = build(data, ResultCodeEnum.SUCCESS);
        return result;
    }

    //失败
    public static Result fail(){
        Result result = build(null, ResultCodeEnum.FAIL);
        return result;
    }

    public static<T> Result<T> fail(T data){
        Result<T> result = build(data, ResultCodeEnum.FAIL);
        return result;
    }

}
